#!/usr/bin/env python
#coding: utf-8
'''
Created on 2017年2月17日

@author: Administrator
'''
from common.models.abstract import TimeModel, StateModel
from django.contrib.auth.models import  User
from django.db import models
from account import settings
from common.utils.storage import get_absolute_url


class Group(TimeModel, StateModel):
    name = models.CharField(default="", max_length=32, verbose_name=u"分组名称")
    remark = models.CharField(default="", max_length=256, verbose_name=u"备注")
    parent = models.ForeignKey("self", null=True, blank=True, verbose_name=u"父分组")
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        app_label = "account"
        verbose_name = u"分组"
        verbose_name_plural = u"分组"
        
class UserGroup(TimeModel, StateModel):
    user = models.ForeignKey(User, verbose_name=u"用户")
    group = models.ForeignKey(Group, verbose_name=u"所属分组")
    is_admin = models.BooleanField(default=False, verbose_name=u"是否该分组管理员")

    class Meta:
        app_label = "account"
        verbose_name = u"用户分组"
        verbose_name_plural = u"用户分组"
        
        
class Profile(StateModel, TimeModel):
    """
    User类的扩展
    """
    user = models.OneToOneField(User, primary_key=True, verbose_name=u'主账号')
    utype = models.PositiveSmallIntegerField(default=settings.UserTypeEnum.DEFAULT, db_index=True, choices=settings.USER_TYPE_CHOICES, verbose_name=u'用户类型')
    sex = models.CharField(max_length=2, default=settings.UserSexEnum.UNKOWN, choices=settings.USER_SEX_CHOICES, blank=True, null=True, verbose_name=u'性别')
    mobile = models.CharField(default=u'', max_length=15, blank=True, null=True, db_index=True, verbose_name=u'移动电话')
    identity_no = models.CharField(default=u'', max_length=18, blank=True, verbose_name=u'身份证号码')
    wechat = models.CharField(default=u'', max_length=32, blank=True, verbose_name=u'微信')
    qq = models.CharField(default=u'', blank=True, max_length=15, verbose_name=u'QQ')
    weibo = models.CharField(default=u'', blank=True, max_length=128, verbose_name=u'微博主页')
    email_authed = models.BooleanField(default=False, verbose_name=u'email认证状态')
    mobile_authed = models.BooleanField(default=False, verbose_name=u'mobile认证状态')
    identity_authed = models.BooleanField(default=False, verbose_name=u'身份认证状态')
    address = models.CharField(default=u'', max_length=64, blank=True, verbose_name=u'地址')
    avatar_fpath = models.CharField(default=settings.DEFAULT_AVATAR_FPATH, max_length=128, verbose_name=u'头像文件')
    origin_avatar_fpath = models.CharField(default=settings.DEFAULT_AVATAR_FPATH, max_length=128, verbose_name=u'头像原文件')
    intro = models.TextField(default=u'', blank=True, verbose_name=u'个人简介')
    avatar_source_url = models.CharField(default=u'', max_length=2048, verbose_name=u'头像来源url')
    
    class Meta:
        app_label = "account"
        verbose_name = u'用户基本资料'
        verbose_name_plural = u'用户基本资料'
        
    def __unicode__(self):
        return u'%s[详细信息]' % self.user.email

    @property
    def avatar_url(self):
        u'''头像地址'''
        if self.avatar_fpath and self.avatar_fpath != settings.DEFAULT_AVATAR_FPATH:
            return get_absolute_url(self.avatar_fpath)
#        elif self.origin_avatar_fpath and self.avatar_fpath != settings.DEFAULT_AVATAR_FPATH:
#            return get_absolute_url(self.origin_avatar_fpath)
        else:
            return "%s%s" % (settings.STATIC_URL, settings.DEFAULT_AVATAR_FPATH)






